package com.jwd.avmanager.client;

import com.jwd.avmanager.constant.Cmd;
import com.jwd.avmanager.entity.MsgPacket;
import com.jwd.avmanager.util.ByteUtil;

import java.io.UnsupportedEncodingException;

/**
 * 根据协议去创建数据包
 */
public class MessageFactory {

    public static final int DEV_IDX = 0;
    public static final short SERIAL_NO = 0;

    /**
     * 组装登录信令包
     *
     * @param deviceNo 设备账号
     * @param userNo   用户账号
     * @param imei     imei号
     * @return 协议包
     */
    public static MsgPacket getLoginPacket(String deviceNo, String userNo, String imei) {
        MsgPacket msgPacket = new MsgPacket(Cmd.CMD_IN_LOGIN, SERIAL_NO, 74, DEV_IDX);
        byte[] deviceNoBytes = deviceNo.getBytes();//占16个字节
        byte[] userNoBytes = userNo.getBytes();//占16个字节
        byte[] imeiBytes = imei.getBytes();//30个字节
        byte[] body = new byte[16 + 16 + 30];
        //注意，拷贝的时候，不能按照实际长度分界，要按照协议来
        System.arraycopy(deviceNoBytes, 0, body, 0, deviceNoBytes.length);
        System.arraycopy(userNoBytes, 0, body, 16, userNoBytes.length);
        System.arraycopy(imeiBytes, 0, body, 32, deviceNoBytes.length);
        msgPacket.putBody(body);
        return msgPacket;
    }

    public static MsgPacket getGpsPacket() {
        MsgPacket msgPacket = new MsgPacket(Cmd.CMD_IN_REQUEST_GPS, SERIAL_NO, 42, DEV_IDX);
        int byteNowSec = 1970;                  //从1970-01-01 00:00:00 开始的秒数
        int byteNowUsec = 300933;               //毫秒数
        short byteUsAngle = 22;                    //夹角，扩大100倍
        short byteUsSpeed = 23;                 //速度扩大了100倍,0xFFFF表示无效(千米/小时)
        char byteUcLongitudeDegree = 'A';        //经度值之度
        char byteUcLongitudeScore = 'A';        //经度值之分
        char byteUcLatitudeDegree = 'B';        //纬度值之度
        char byteUcLatitudeScore = 'B';            //纬度值之分
        int byteUnLongitudeSecond = 107;        //经纬度之秒,扩大了10的7次方倍
        int byteUnLatitudeSecond = 107;            //纬度值值秒扩大了10的7次方倍
        short byteUsHigh = 221;                //海拔高度0xFFFF表示无效
        short byteUsTempera = 223;                //温度0xFFFF表示无效(摄氏度)
        char byteUcSatelliteNumber = 'C';        //卫星数量0xFF表示无效
        char byteUcStatus = 'D';                //Bit0:GPS数据有效性(0:无效,1:有效)
        //Bit1:经度方向(0:东经,1:西经)
        //Bit2:纬度方向(0:南纬,1:北纬)
        //Bit3~Bit7:保留

//        byte[] a = byteNowSec.getBytes();
//        byte[] b = byteNowUsec.getBytes();
//        byte[] c = byteUsAngle.getBytes();
//        byte[] d = byteUsSpeed.getBytes();
//        byte[] e = byteUcLongitudeDegree.getBytes();
//        byte[] f = byteUcLongitudeScore.getBytes();
//        byte[] g = byteUcLatitudeDegree.getBytes();
//        byte[] h = byteUcLatitudeScore.getBytes();
//        byte[] i = byteUnLongitudeSecond.getBytes();
//        byte[] g = byteUnLatitudeSecond.getBytes();
//        byte[] k = byteUsHigh.getBytes();
//        byte[] l = byteUsTempera.getBytes();
//        byte[] n = byteUcSatelliteNumber.getBytes();
//        byte[] m = byteUcStatus.getBytes();

        return msgPacket;
    }

    public static MsgPacket getTalkbackCallT(String ID) {
        MsgPacket msgPacket = new MsgPacket(Cmd.CMD_IN_CALL_PERSONNEL_LIST, SERIAL_NO, 28, DEV_IDX);
        byte[] getID = ID.getBytes();//占16个字节
        byte[] body = new byte[16];
        System.arraycopy(getID, 0, body, 0, getID.length);
        msgPacket.putBody(body);
        return msgPacket;
    }

    public static MsgPacket getUser(String ID) {
        MsgPacket msgPacket = new MsgPacket(Cmd.CMD_IN_TALKBACK_PERSONNEL, SERIAL_NO, 28, DEV_IDX);
        byte[] getID = ID.getBytes();//占16个字节
        byte[] body = new byte[16];
        System.arraycopy(getID, 0, body, 0, getID.length);
        msgPacket.putBody(body);
        return msgPacket;
    }


    public static MsgPacket getGroup(String ID) {
        MsgPacket msgPacket = new MsgPacket(Cmd.CMD_IN_TALKBACK_GROUP, SERIAL_NO, 28, DEV_IDX);
        byte[] getID = ID.getBytes();//占16个字节
        byte[] body = new byte[16];
        System.arraycopy(getID, 0, body, 0, getID.length);
        msgPacket.putBody(body);
        return msgPacket;
    }

    public static MsgPacket getIE(char[] ID) {
        MsgPacket msgPacket = new MsgPacket(Cmd.CMD_IN_IEMI_REGISTER, SERIAL_NO, 48, DEV_IDX);
        byte[] b = new byte[0];
        try {
            b = new String(ID).getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] body = new byte[32];
        System.arraycopy(b, 0, body, 0, b.length);
        msgPacket.putBody(body);
        return msgPacket;
    }

    /**
     * GPS
     */
    public static MsgPacket setGPS(int ID) {
        MsgPacket msgPacket = new MsgPacket(Cmd.CMD_IN_REQUEST_GPS, SERIAL_NO, 48, DEV_IDX);
        int now_sec = ID;               //从1970-01-01 00:00:00 开始的秒数
        int now_usec = 232;              //毫秒数
        short usAngle = 12;                //夹角，扩大100倍
        short usSpeed = 180;                //速度扩大了100倍,0xFFFF表示无效(千米/小时)
        char ucLongitude_degree = 'a';    //经度值之度
        char ucLongitude_Score = 'b';        //经度值之分
        char ucLatitude_degree = 'c';        //纬度值之度
        char ucLatitude_Score = 'd';        //纬度值之分
        int unLongitude_Second = 444;    //经纬度之秒,扩大了10的7次方倍
        int unLatitude_Second = 555;        //纬度值值秒扩大了10的7次方倍
        short usHigh = 33;                //海拔高度0xFFFF表示无效
        short usTempera = 66;            //温度0xFFFF表示无效(摄氏度)
        char ucSatelliteNumber = 'e';        //卫星数量0xFF表示无效
        char ucStatus = 'f';                //Bit0:GPS数据有效性(0:无效,1:有效)
        //Bit1:经度方向(0:东经,1:西经)
        //Bit2:纬度方向(0:南纬,1:北纬)
        //Bit3~Bit7:保留
        byte[] byte_sec = ByteUtil.intToBytes(now_sec);
        byte[] byte_usec = ByteUtil.intToBytes(now_usec);
        byte[] byte_usAngle = ByteUtil.shortToBytes(usAngle);
        byte[] byte_usSpeed = ByteUtil.shortToBytes(usSpeed);
        byte[] byte_degree = ByteUtil.charToByte(ucLongitude_degree);
        byte[] byte_Score = ByteUtil.charToByte(ucLongitude_Score);
        byte[] byte_degree2 = ByteUtil.charToByte(ucLatitude_degree);
        byte[] bytes_Score = ByteUtil.charToByte(ucLatitude_Score);
        byte[] bytes = ByteUtil.intToBytes(unLongitude_Second);
        byte[] bytes1 = ByteUtil.intToBytes(unLatitude_Second);
        byte[] bytes2 = ByteUtil.shortToBytes(usHigh);
        byte[] bytes3 = ByteUtil.shortToBytes(usTempera);
        byte[] bytes4 = ByteUtil.charToByte(ucSatelliteNumber);
        byte[] bytes5 = ByteUtil.charToByte(ucStatus);
        int a = byte_sec.length + byte_usec.length + byte_usAngle.length + byte_usSpeed.length + byte_degree.length + byte_Score.length + byte_degree2.length + bytes_Score.length + bytes.length + bytes1.length + bytes2.length + bytes3.length + bytes4.length + bytes5.length;
        byte[] body = new byte[36];
        System.arraycopy(byte_sec, 0, body, 0, byte_sec.length);
        System.arraycopy(byte_usec, 0, body, 4, byte_usec.length);
        System.arraycopy(byte_usAngle, 0, body, 8, byte_usAngle.length);
        System.arraycopy(byte_usSpeed, 0, body, 10, byte_usSpeed.length);
        System.arraycopy(byte_degree, 0, body, 12, byte_degree.length);
        System.arraycopy(byte_Score, 0, body, 14, byte_Score.length);
        System.arraycopy(byte_degree2, 0, body, 16, byte_degree2.length);
        System.arraycopy(bytes_Score, 0, body, 18, bytes_Score.length);
        System.arraycopy(bytes, 0, body, 20, bytes.length);
        System.arraycopy(bytes1, 0, body, 24, bytes1.length);
        System.arraycopy(bytes2, 0, body, 28, bytes2.length);
        System.arraycopy(bytes3, 0, body, 30, bytes3.length);
        System.arraycopy(bytes4, 0, body, 32, bytes4.length);
        System.arraycopy(bytes5, 0, body, 34, bytes5.length);
        msgPacket.putBody(body);
        return msgPacket;
    }

    /**
     * 获取心跳包
     *
     * @return
     */
    public static MsgPacket getHeartbeatPacket() {
        MsgPacket msgPacket = new MsgPacket(Cmd.CMD_IN_HEARTBEAT, SERIAL_NO, 12, DEV_IDX);
        return msgPacket;
    }
}
